<template>
	<scroll-view class="apply-for" :style="{color: color}" :refresher-enabled="true" :refresher-triggered="triggered"
		:refresher-threshold="100" @refresherrefresh="refresherpulling">
		<view class="apply-detail" v-if="applyStatus == 0 || applyStatus == 3">
			<view class="apply-info">
				<view class="info-label">
					{{title}}
				</view>
				<view class="info-input">
					<input class="uni-input" v-model="companyName" :placeholder="placeholder" v-if="type == 'input'" />
					<uni-data-select v-model="positionType" :localdata="range" :placeholder="placeholder"
						@change="change" v-else></uni-data-select>
				</view>
			</view>
			<button @click="submit">提交</button>
			<view class="agreement">
				<view class="agreement-model">
					<u-checkbox-group @change="changeAgreement" class="radio-box">
						<u-checkbox :model="agreement" shape="circle" label=" " name="1"></u-checkbox>
					</u-checkbox-group>

					<view class="">
						<text :style="{color: color}">请先阅读隐私协议，勾选则代表同意协议内容</text>

						<text class="agreement-name" @click.stop="openPrivacy">《消易云服务隐私协议》</text>
						<!-- <text class="agreement-name" @click.stop="openPrivacy">《{{protocolTitle}}》</text> -->
					</view>

				</view>
			</view>
		</view>
		<!-- <view class="apply-detail" style="font-size: 40rpx;" v-else>审核中...</view> -->
		<image :src="image" mode="" class="apply-img"></image>
	</scroll-view>
</template>

<script>
	import Apply from '@/api/apply.js'
	import Common from "@/api/common.js"
	export default {
		data() {
			return {
				value: null,
				range: [{
						value: 0,
						text: "工程师"
					},
					{
						value: 1,
						text: "操作员"
					},
				],
				applyStatus: 0,
				positionType: null,
				agreement: false,
				companyName: '',
				agreementsContent: '',
				protocolTitle: '',
				triggered: false,
				rangeClickValue: {}
			}
		},
		options: {
			styleIsolation: 'shared'
		},
		props: {
			image: {
				default: '',
				type: String,
			},
			title: {
				default: '信息',
				type: String
			},
			placeholder: {
				default: '请输入内容',
				type: String
			},
			color: {
				default: '',
				type: String
			},
			type: {
				default: 'input',
				type: String
			}
		},
		mounted() {
			this.getRoleType()
			this.getAgreements()
			this.getApplyStatus()
		},
		methods: {
			change(v) {
				if (this.type != 'input') {
					this.rangeClickValue = this.range.find(el => el.value == v)
					console.log(this.rangeClickValue, '1111');
				}
			},
			changeAgreement(v) {
				if (v.length) {
					this.agreement = true
				} else {
					this.agreement = false
				}
			},
			// 工程师申请类型
			getRoleType() {
				Apply.roleType().then(res => {
					if (res.code == 200) {
						console.log(res, 'res');
						this.range = []
						res.data.forEach((item, i) => {
							this.range.push({
								positionType: item.dictValue,
								value: i + 1,
								text: item.dictLabel,
							})
						})
					}
				})
			},
			submit() {
				this.type == 'input' ? this.setOrganizationEnterApply() : this.setEngineerApply()
			},
			// 工程师申请
			setEngineerApply() {
				if (!this.agreement) {
					uni.showToast({
						title: '请先阅读隐私协议并勾选',
						icon: 'none',
					})
					return
				}
				if (!this.rangeClickValue) {
					uni.showToast({
						title: '请选择申请类型',
						icon: 'none',
					})
					return
				}
				Apply.engineerApply({
					positionType: this.rangeClickValue.positionType,
					applicantType: this.rangeClickValue.value
				}).then(res => {
					if (res.code == 200) {
						uni.showToast({
							title: '申请成功',
						})
						this.getApplyStatus()
						// setTimeout(() => {
							// uni.reLaunch({
							// 	url: "/pages/home"
							// })
						// }, 1000)
						// this.getApplyStatus()
					}
				})
			},
			// 机构入驻申请 
			setOrganizationEnterApply() {
				if (!this.agreement) {
					uni.showToast({
						title: '请先阅读隐私协议并勾选',
						icon: 'none',
					})
					return
				}
				Apply.organizationApply({
					companyName: this.companyName
				}).then(res => {
					if (res.code == 200) {
						uni.showToast({
							title: '申请成功',
						})
						setTimeout(() => {
							uni.reLaunch({
								url: "/pages/home"
							})
						}, 1000)
						// this.getApplyStatus()
					}
				})
			},
			// 当前账号申请状态
			async getApplyStatus() {
				let res
				if (this.type == 'input') {
					res = await Apply.organizationStatus()
				} else {
					res = await Apply.engineerStatus()
				}
				if (res.code == 200) {
					// 0未申请 1审核中 2通过 3未通过
					if (res.data) {
						this.applyStatus = res.data.auditType
						if (res.data.auditType == 2) {
							this.$emit('applySuccess', res.data)
						} else if (res.data.auditType == 1) {
							uni.redirectTo({
								url: `/subPages/home/master/notExamine?title=${this.type == 'input' ?'企业入驻':'工程师入驻'}&msg=请您耐心等待`
							})
							uni.setStorageSync('toUrl', this.type == 'input' ? '/subPages/home/organizationEnter' :
								'/subPages/home/engineer')
						}
					} else {
						this.applyStatus = 0
					}
				}
			},
			formatterImage(data) {
				if (data == "") return "";
				var jsonDa = JSON.stringify(data).replace(/<img/gi,
					"<img class='richImg'style='width:100%!important;height:auto!important;max-height:100%;'");
				var newResData = JSON.parse(jsonDa);
				return newResData;
			},
			async getAgreements() {
				let res = await Common.getAgreements(this.type == 'input' ? '11' : '10');
				this.protocolTitle = res.data.title
				this.agreementsContent = this.formatterImage(res.data?.content);
			},
			openPrivacy() {
				uni.navigateTo({
					url: `/subPages/mine/publicAgreement?id=${this.type == 'input' ? '11' : '10'}`
				})
			},
			refresherpulling() {
				this.triggered = true
				this.getApplyStatus()
				setTimeout(() => {
					this.triggered = false
				}, 1000)
			}
		}
	}
</script>

<style lang="scss" scoped>
	.apply-for {
		width: 100vw;
		height: 100vh;

		.apply-img {
			width: 100%;
			height: 100%;
		}

		// background-size: 100% 100%;
		// background-position: center;
		// background-repeat: no-repeat;

		.apply-detail {
			height: 25%;
			width: 100%;
			position: absolute;
			bottom: 0;
			left: 0;
			z-index: 99;
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: space-between;
			padding: 30rpx 10%;
			padding-bottom: 50rpx;

			.apply-info {
				display: flex;
				align-items: center;
				width: inherit;

				.info-label {
					margin-right: 20rpx;
				}

				.info-input {
					flex: 1;
					width: 100%;


					.uni-input {
						background-color: #fff;
						color: black;
						height: 80rpx;
						width: inherit;
						border-radius: 15rpx;
						border: 1px solid #999;
						padding-left: 10rpx;
					}

					.info-select {
						background-color: #fff !important;
					}

				}
			}

			button {
				width: 50%;
				height: 80rpx;
				background-color: $main-color;
				color: #fff;
				line-height: 80rpx;
			}

			.agreement {
				.agreement-model {
					display: flex;
					width: 100%;

					/deep/.radio-box {
						width: 50rpx;
						height: 50rpx;

						.raio-item {}
					}

					text {
						font-size: 24rpx;
						color: $main-font-auxiliary;
					}

					.agreement-name {
						color: $main-color;
					}

				}
			}
		}
	}

	/deep/.uni-select {
		background-color: #fff !important;
	}
</style>